package com.qingchi.web.controller.app;

import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
import com.socialuni.social.model.common.ResultRO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.script.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

/**
 * @author qinkaiyuan
 * @date 2019-09-28 11:09
 * 前端初始化内容
 */
@RestController
@RequestMapping("test")
public class TestControllerController {

    @RequestMapping("")
    public ResultRO<Object> testUrl(String content) throws IOException, ScriptException {
        // Graal
        ScriptEngine engine = GraalJSScriptEngine.create();
        File file = ResourceUtils.getFile("classpath:social/socialAPIConfig.mjs");
        engine.eval(new FileReader(file));
        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        Map<Object, Object> map = (Map) bindings.get("javaObj");
        for (Map.Entry entry : map.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }


        /*ScriptEngine engine = GraalJSScriptEngine.create(null,
                Context.newBuilder("js")
                        .allowHostAccess(HostAccess.ALL)
                        .allowHostClassLookup(s -> true)
                        .option("js.ecmascript-version", "2020"));
        engine.put("javaObj", new Object());
        engine.eval("(javaObj instanceof Java.type('java.lang.Object'));");*/

        /*Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        Map<Object,Object> map = (Map) bindings.get("javaObj");
        for (Map.Entry entry:map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }*/
        /*File file = ResourceUtils.getFile("classpath:social/socialAPIConfig.mjs");

        System.out.println("warming up ...");

        ScriptEngine engine = GraalJSScriptEngine.create(null,
                Context.newBuilder("js")
        );
        engine.eval(new FileReader(file));
        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        Map<Object,Object> map = (Map) bindings.get("socialAPIConfig");
        for (Map.Entry entry:map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }*/

        /*val scriptOutput = new StringWriter()
        engine.getContext.setWriter(scriptOutput)

        engine.eval("print('Test åäö !');")
        val out = scriptOutput.toString
        println(out);

        ScriptEngine engine = GraalJSScriptEngine.create(null,
                Context.newBuilder("js")
                        .allowHostAccess(HostAccess.ALL)
                        .allowHostClassLookup(s -> true)
                        .option("js.ecmascript-version", "2021"));
        engine.put("javaObj", new Object());
        engine.eval("(javaObj instanceof Java.type('java.lang.Object'));");*/
        /*获取执行JavaScript的执行引擎*/
//        ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
        /*为文件注入全局变量*/
        /*设置绑定参数的作用域*/
        /*engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        // 获得js文件

        System.out.println(bindings.get("socialAPIConfig"));
        Map<Object,Object> map = (Map) bindings.get("socialAPIConfig");
        for (Map.Entry entry:map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
*/
//        System.out.println(list);

        return new ResultRO<>();
    }


    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;


    @RequestMapping("upload")
    public ResultRO<Object> testUpload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file);
        System.out.println(file);
        //校验图片是否合法
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOAD_FOLDER + file.getOriginalFilename());
            //如果没有files文件夹，则创建
            if (!Files.isWritable(path)) {
                Files.createDirectories(Paths.get(UPLOAD_FOLDER));
            }
            //文件写入指定路径
            Files.write(path, bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(123123);
        System.out.println(123123);

        return new ResultRO<>();
    }

}
